package com.runa.rccprotocol.dto.apportion;

import com.runa.protocol.dto.IResponseData;
import com.runa.protocol.enums.ByteOrder;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.ConcentratorRequestData;

public class ApportionRequestData extends ConcentratorRequestData {

    private byte afn;

    private int dt;

    private byte[] data;

    protected ApportionRequestData(String address) {
        super(address);
    }

    public void openValve(String valve) {
        this.afn = 0x13;
        this.dt = 0x200;
        this.data = ByteUtil.hexStringToByteFlip(valve);
    }

    public void closeValve(String valve) {
        this.afn = 0x13;
        this.dt = 0x400;
        this.data = ByteUtil.hexStringToByteFlip(valve);
    }

    public void forceOpen(String valve) {
        this.afn = 0x13;
        this.dt = 0x800;
        this.data = ByteUtil.hexStringToByteFlip(valve);
    }

    public void forceClose(String valve) {
        this.afn = 0x13;
        this.dt = 0x1000;
        this.data = ByteUtil.hexStringToByteFlip(valve);
    }

    public void openModel(String valve) {
        this.afn = 0x13;
        this.dt = 0x8009;
        this.data = ByteUtil.hexStringToByteFlip(valve);
    }

    public void setOpen(String valve, int open) {
        this.afn = 0x13;
        this.dt = 0x8008;
        this.data = ByteUtil.merge(ByteUtil.hexStringToByteFlip(valve), new byte[]{(byte) open});
    }

    public void tempModel(String valve) {
        this.afn = 0x13;
        this.dt = 0x2008;
        this.data = ByteUtil.hexStringToByteFlip(valve);
    }

    public void setRoomTemp(String valve, float temp) {
        this.afn = 0x13;
        this.dt = 2009;
        int integer = (int) temp;
        int fractional = (int) ((temp - integer) * 100);
        this.data = ByteUtil.merge(ByteUtil.hexStringToByteFlip(valve), new byte[]{0x55, (byte) fractional, (byte) integer, 1, 0});
    }

    public void setReturnTemp(String valve, float temp) {
        this.afn = 0x13;
        this.dt = 2009;
        int integer = (int) temp;
        int fractional = (int) ((temp - integer) * 100);
        this.data = ByteUtil.merge(ByteUtil.hexStringToByteFlip(valve), new byte[]{0x77, (byte) fractional, (byte) integer, 1, 0});
    }

    public void setAvgTemp(String valve, float temp) {
        this.afn = 0x13;
        this.dt = 2009;
        int integer = (int) temp;
        int fractional = (int) ((temp - integer) * 100);
        this.data = ByteUtil.merge(ByteUtil.hexStringToByteFlip(valve), new byte[]{0x66, (byte) fractional, (byte) integer, 1, 0});
    }

    public void setTemp(String valve, float temp) {
        this.afn = 0x13;
        this.dt = 0x2000;
        this.data = ByteUtil.merge(ByteUtil.hexStringToByteFlip(valve), new byte[2]);
        ByteUtil.shortToByte((short) (temp * 100), data, 5, ByteOrder.BA);
    }

    public void readMeter(String meter) {
        this.afn = 0xc;
        this.dt = 0x4003;
        this.data = ByteUtil.hexStringToByteFlip(meter);
    }

    public void readValve(String valve) {
        this.afn = 0xc;
        this.dt = 0x2003;
        this.data = ByteUtil.hexStringToByteFlip(valve);
    }

    @Override
    public boolean check(IResponseData data) {
        return true;
    }

    @Override
    public byte[] getData() {
        byte[] bytes = new byte[20 + data.length];
        bytes[0] = 0x68;
        int len = 12 + data.length;
        ByteUtil.shortToByte((short) len, bytes, 1, ByteOrder.BA);
        ByteUtil.shortToByte((short) len, bytes, 3, ByteOrder.BA);
        bytes[5] = 0x68;
        bytes[6] = 0x41;
        bytes[7] = 0x02;
        bytes[8] = 0x37;
        bytes[9] = 0x01;
        bytes[12] = afn;
        bytes[13] = (byte) 0xe0;
        ByteUtil.shortToByte((short) dt, bytes, 16, ByteOrder.AB);
        System.arraycopy(data, 0, bytes, 18, data.length);
        bytes[bytes.length - 2] = ByteUtil.sum(bytes, 6, len);
        bytes[bytes.length - 1] = 0x16;
        return bytes;
    }

    public static void main(String[] args) {
        ApportionRequestData requestData = new ApportionRequestData(null);
        requestData.readMeter("2112241001");
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));

        requestData.readValve("2112240003");
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));

        requestData.openValve("2112240003");
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));

        requestData.closeValve("2112240003");
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));

        requestData.setOpen("2112240003", 50);
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));

        requestData.setTemp("2112240003", (float) 18.5);
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));

        requestData.openModel("2112240003");
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));

        requestData.setOpen("2112240003", 50);
        System.out.println(ByteUtil.byteToHexString(requestData.getData()));
    }
}
